<?php

namespace App\Services;

use App\Repositories\Contracts\OrderAuditLogRepository;

class OrderAuditLogService
{
    private $orderAuditLogRepository;

    public function __construct(OrderAuditLogRepository $orderAuditLogRepository)
    {
        $this->orderAuditLogRepository = $orderAuditLogRepository;
    }

    /**
     * 获取列表
     *
     * @param array|null $param
     */
    public function getList($param = null)
    {
        try {
            $filter = [];
            $result = $this->orderAuditLogRepository->paginate($param['per_page'] ?? null, $filter);
            return $result;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    public function getMyLog($param)
    {
        try {
            $meetLog = 0;
            $activityLog = 0;
            $volunteerLog = 0;

            //场馆预约
            $meetWhere = [
                'status' => 1,
                'type' => 1,
                'audit_user_id' => $param['add_uid'],
            ];

            $meet = $this->orderAuditLogRepository->findByFilter($meetWhere);
            if ($meet) {
                $meetLog = $meet->id;
            }
            //活动预约
            $activityWhere = [
                'status' => 1,
                'type' => 2,
                'audit_user_id' => $param['add_uid'],
            ];
            $activity = $this->orderAuditLogRepository->findByFilter($activityWhere);
            if ($activity) {
                $activityLog = $activity->id;
            }

            //志愿服务
            $volunteerWhere = [
                'status' => 1,
                'type' => 3,
                'audit_user_id' => $param['add_uid'],
            ];
            $volunteer = $this->orderAuditLogRepository->findByFilter($volunteerWhere);
            if ($volunteer) {
                $volunteerLog = $volunteer->id;
            }
            return [
                'meetLog' => $meetLog,
                'activityLog' => $activityLog,
                'volunteerLog' => $volunteerLog,
            ];
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    public function submit($param)
    {
        try {
            //查找该id记录
            $orderAuditLog = $this->orderAuditLogRepository->find($param['id']);
            //查询哪个类型，同一类型同帐号状态全改为0
            if ($orderAuditLog && $orderAuditLog->status != 0) {
                $where = [
                    'type' => $orderAuditLog->type,
                    'audit_user_id' => $param['add_uid'],
                    'status' => 1,
                ];
                $this->orderAuditLogRepository->updateByFilter(['status' => 0], $where);
            }
            return 'success';
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 获取详情
     *
     * @param int $id
     * @return object|array
     */
    public function getInfo($id)
    {
        try {
            $result = $this->orderAuditLogRepository->find($id);
            return $result;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 新增
     *
     * @param array $param
     */
    public function add($param)
    {
        try {
            $orderAuditLog = $this->orderAuditLogRepository->store($param);
            return $orderAuditLog;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 修改
     *
     * @param array $param
     * @param int $id
     */
    public function update($param, $id)
    {
        try {
            $orderAuditLog = $this->orderAuditLogRepository->update($param, $id);
            return $orderAuditLog;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 删除
     *
     * @param int $id
     */
    public function delete($id)
    {
        try {
            $orderAuditLog = $this->orderAuditLogRepository->delete($id);

            $result = [];
            if ($orderAuditLog) {
                $result = 'success';
            } else {
                $result = 'error';
            }
            return $result;

        } catch (\Throwable $th) {
            throw $th;
        }
    }
}
